package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.Ext.Companion.OBSINFO
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.Child
import com.lancoo.znbkxx.teacher.model.data.ChildClassical
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.item_stu_answer.view.*

/**
 * @author : wealive
 * @date   : 2020/3/17 8:52
 * @desc   :
 */
class AnswerListAdapter(
        layoutResId: Int,
        data: MutableList<ChildClassical>,
        private val onAudioClickListener: OnAudioClickListener,
        val index: Int
) :
        BaseQuickAdapter<ChildClassical, BaseViewHolder>(layoutResId, data) {
    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: ChildClassical?) {
        with(helper.itemView) {
            if (item!!.ItemList.size == 1) {
                val imagePath = item.ItemList[0].ImagePath
                val audioPath = item.ItemList[0].AudioPath
                val stuAnswer = item.ItemList[0].StuAnswer
                when {
                    imagePath.isNotBlank() -> {
                        tv_answer.visibility = View.VISIBLE
                        tv_answer.text = "${helper.layoutPosition + 1}.${stuAnswer}"
                        rv_image.visibility = View.VISIBLE
                        iv_audio.visibility = View.GONE
                        val pathList = item.ItemList[0].ImagePath.split("|")
                                .dropLastWhile { it.isBlank() }.toMutableList()
                        val mediaList = mutableListOf<LocalMedia>()
                        pathList.forEach {
                            val media = LocalMedia()
                            media.path = OBSINFO?.ServerWithBucket + it
                            media.mimeType = "image/" + it.substring(it.lastIndexOf(".") + 1)
//                            media.mimeType = 1
                            mediaList.add(media)
                        }
                        val adapter = StuImageAdapter(R.layout.item_image, mediaList)
                        adapter.onItemClickListener = this@AnswerListAdapter.onItemClickListener
                        rv_image.layoutManager = GridLayoutManager(context, 3)
                        rv_image.addItemDecoration(
                                EvenItemDecoration(
                                        DensityUtils.dp2px(context, 5f),
                                        3
                                )
                        )
                        rv_image.adapter = adapter
                    }
                    audioPath.isNotBlank() -> {
                        iv_audio.setOnClickListener {
                            onAudioClickListener.success(
                                    iv_audio,
                                    index,
                                    helper.layoutPosition
                            )
                        }
                        if (stuAnswer.isNotBlank()) {
                            tv_answer.visibility = View.VISIBLE
                            tv_answer.text = stuAnswer
                        } else {
                            tv_answer.visibility = View.GONE
                        }
                        rv_image.visibility = View.GONE
                        iv_audio.visibility = View.VISIBLE
                    }
                    stuAnswer.isNotBlank() -> {
                        tv_answer.visibility = View.VISIBLE
                        if (data.size == 1) {
                            tv_answer.text = stuAnswer
                        } else {
                            tv_answer.text = "${helper.layoutPosition + 1}.${stuAnswer}"
                        }
                        rv_image.visibility = View.GONE
                        iv_audio.visibility = View.GONE
                    }
                    else -> {
                        tv_answer.visibility = View.VISIBLE
                        if (data.size == 1) {
                            tv_answer.text = "未作答"
                        } else {
                            tv_answer.text = "${helper.layoutPosition + 1}.未作答"
                        }
                        rv_image.visibility = View.GONE
                        iv_audio.visibility = View.GONE
                    }
                }
            } else {
                val answerList = mutableListOf<String>()
                item.ItemList.forEach {
                    if (it.StuAnswer.isEmpty()) {
                        answerList.add("未作答")
                    } else {
                        answerList.add(it.StuAnswer)
                    }
                }
                if (data.size <= 1) {
                    val answer = StringBuffer()
                    answerList.forEachIndexed { index, s ->
                        answer.append("${index + 1}.${s}\n")
                    }
                    tv_answer.text = answer
                } else {
                    val answer = answerList.joinToString("、")
                    tv_answer.text = "${helper.layoutPosition + 1}.${answer}"
                }
                tv_answer.visibility = View.VISIBLE
                rv_image.visibility = View.GONE
                iv_audio.visibility = View.GONE
            }
        }
    }

    interface OnAudioClickListener {
        //成功的方法传 int 的索引
        fun success(view: ImageView, index: Int, position: Int)
    }
}